/**
 * 
 */
package net.plankton.converter;

import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author hacklm
 *
 */
public class AviConverter extends DirectoryWatcher {

	
	
	public AviConverter(String rootDir,String copyDir) {
		super(rootDir);
		this.copyDir=copyDir;
	}

	String copyDir;
	
	/* (non-Javadoc)
	 * @see net.plankton.converter.DirectoryWatcher#handle(java.io.File)
	 */
	@Override
	public void handle(File f) {
		System.err.println(f.getAbsolutePath());
		StringBuilder mencoder = new StringBuilder();
		String[] options = new String[6];
//		options[0]="/usr/bin/mencoder";
		
		mencoder.append("-ovc lavc -lavcopts vcodec=mjpeg −vf pp=fd -nosound ")
		.append(f.getAbsoluteFile())
		.append(" -o ")
		.append(copyDir)
		.append(f.getName());
		System.err.println(mencoder.toString());
		Runtime r = Runtime.getRuntime();
		
		try {
			List<String> command = new ArrayList<String>();
		    command.add("/video/convert.sh");
		    command.add(f.getAbsolutePath());
		    command.add(copyDir+f.getName());

		    ProcessBuilder builder = new ProcessBuilder(command);
		    
		    Map<String, String> environ = builder.environment();
		    builder.directory(new File("/tmp"));


		    final Process process = builder.start();
		    InputStream is = process.getInputStream();
		    InputStreamReader isr = new InputStreamReader(is);
		    BufferedReader br = new BufferedReader(isr);
		    String line;
		    while ((line = br.readLine()) != null) {
		      System.out.println(line);
		    }
		    System.out.println("Program terminated!");
		  
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	public static void main(String[] args) {
		new AviConverter("/home/baco/convert","/video/converted/").start();
	}
	
}
